//
//  LGSettingViewController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/6/4.
//  设置

#import "LGSettingViewController.h"
#import "LGCheckPasswordController.h"
#define lineHeight  viewPix(55)

@interface LGSettingViewController ()

/**  专家咨询 */
@property (nonatomic , strong)UISwitch *consultSwitch;

@end

@implementation LGSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    self.view.backgroundColor = [UIColor colorWithString:@"#F6F6F6"];
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), lineHeight*3)];
    baseView.backgroundColor = [UIColor whiteColor];
    baseView.cornerRidus = 10;
    [self.view addSubview:baseView];
    
    UILabel *consultTitle = [UILabel lableWithText:@"专家咨询设置" colorString:@"#2B2A37" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    consultTitle.frame = CGRectMake(viewPix(16), 0, Screen_W-viewPix(150), lineHeight);
    [baseView addSubview:consultTitle];
    [baseView addSubview:self.consultSwitch];
    [self.consultSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(baseView).offset(-viewPix(16));
        make.centerY.equalTo(consultTitle);
        make.width.equalTo(@(viewPix(50)));
        make.height.equalTo(@(viewPix(28)));
    }];
    
    UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), lineHeight, Screen_W-viewPix(64), 1)];
    lineView1.backgroundColor = [UIColor colorWithString:@"#EEEEEE"];
    [baseView addSubview:lineView1];
    
    UIButton *passwordBtn = [UIButton buttonWithTitle:@"密码修改" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#2B2A37" frame:CGRectMake(0, lineHeight, Screen_W-viewPix(32), lineHeight)];
    passwordBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    passwordBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(16), 0, 0);
    [passwordBtn addTarget:self action:@selector(changePasswordAction) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:passwordBtn];
    
    UIImageView *passTip = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
    [passwordBtn addSubview:passTip];
    [passTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(passwordBtn).offset(-viewPix(16));
        make.width.height.equalTo(@(viewPix(16)));
        make.centerY.equalTo(passwordBtn);
    }];
    
    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), lineHeight*2, Screen_W-viewPix(64), 1)];
    lineView2.backgroundColor = [UIColor colorWithString:@"#EEEEEE"];
    [baseView addSubview:lineView2];
    
    UIButton *logoutBtn = [UIButton buttonWithTitle:@"退出登录" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#2B2A37" frame:CGRectMake(0, lineHeight*2, Screen_W-viewPix(32), lineHeight)];
    logoutBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    logoutBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(16), 0, 0);
    [logoutBtn addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:logoutBtn];
    
    UIImageView *logoutTip = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
    [logoutBtn addSubview:logoutTip];
    [logoutTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(logoutBtn).offset(-viewPix(16));
        make.width.height.equalTo(@(viewPix(16)));
        make.centerY.equalTo(logoutBtn);
    }];
    self.consultSwitch.on = self.consultOpen;
}

//获取专家咨询开启状态
-(void)requestConsultData{
    
}

-(void)consultSwitchAction:(UISwitch *)sender{
    NSString *open = (sender.on == YES)?@"1":@"0";
    [RequestUtil bodayPOST:@"/brbot-user/update" parameters:@{@"id":kUserId,@"persionalAsk":open} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            
        }else{
            [TooltipView showMessage:message offset:0];
            self.consultSwitch.on = !self.consultSwitch.on;
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"保存失败，请重试" offset:0];
        self.consultSwitch.on = !self.consultSwitch.on;
    }];
}

//修改密码
-(void)changePasswordAction{
    LGCheckPasswordController *controller = [[LGCheckPasswordController alloc]init];
    controller.currentPhone = self.phone;
    [self.navigationController pushViewController:controller animated:YES];
}

//退出登录
-(void)logoutAction{
    [LGAlertTool alertWithTitle:@"退出登录" content:@"您确定要退出登录？" sureAction:^{
        [LGLoginManager logoutAction];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"auth_token"];
    }];
}


-(UISwitch *)consultSwitch{
    if(!_consultSwitch){
        _consultSwitch = [[UISwitch alloc]init];
        _consultSwitch.onTintColor = [UIColor  colorWithString:kThemeColor];
        [_consultSwitch addTarget:self action:@selector(consultSwitchAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _consultSwitch;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
